<template>
  <div class="tags-add">
    <el-dialog
      :title="`${data.id ? '修改' : '新增'}目录`"
      :visible="isShow"
      width="400px"
      @open="handleOpen"
      @close="handleClose"
    >
      <el-form
        label-width="80px"
        ref="form"
        size="small"
        :model="form"
        :rules="rules"
      >
        <el-form-item label="所属学科" prop="subjectID">
          <el-select v-model="form.subjectID" style="width: 100%">
            <el-option
              v-for="item in subjectOptions"
              :value="item.value"
              :label="item.label"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="tagName">
          <el-input
            placeholder="请选输入目录名称"
            v-model="form.tagName"
          ></el-input>
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { simple } from '@/api/hmmm/subjects'
import { add, update } from '@/api/hmmm/tags'
export default {
  name: 'TagsAdd',

  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      // default: () => {}
      default: function () {
        return {}
      }
    }
  },

  data () {
    return {
      form: {
        id: null,
        subjectID: null,
        tagName: null
      },
      // 自定义验证规则
      rules: {
        subjectID: [
          { type: 'number', message: '请选择所属学科', tirgger: 'blur' }
        ],
        tagName: [
          { required: true, message: '目录名称不能为空', trigger: ['blur'] }
        ]
      },
      // 放 所属学科option的容器
      subjectOptions: []
    }
  },

  // 首次渲染
  created () {
    // const { data } = await simple()
    // console.log(data)
    // this.subjectOptions = data.label
    // console.log(this.form)
    // console.log(this.data)
  },

  methods: {
    // 打开对话框时要渲染页面
    async handleOpen () {
      if (this.data.id) {
        console.log('此时是编辑状态', this.data.id)
        console.log(this.data)
        const { id, subjectID, tagName } = this.data
        this.form = { id, subjectID, tagName }
      } else {
        console.log('此时是新增状态', this.data)
        this.form = {
          id: null,
          subjectID: +this.$route.query.id || null,
          tagName: null
        }
      }
      // 清除校验效果
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })

      console.log('对话框打开了')
      const { data } = await simple()
      console.log(data)

      this.subjectOptions = data
      console.log(this.subjectOptions)
    },

    // 关闭对话框
    handleClose () {
      this.$emit('update:isShow', false)
    },

    //
    handleSubmit () {
      this.$refs.form.validate(async (flag) => {
        if (!flag) return

        // 验证通过
        if (!this.data.id) {
          // 新增
          await add(this.form)
        } else {
          // 修改
          this.form.id = this.data.id
          await update(this.form)
        }

        this.$message.success('操作成功')
        // this.show = false
        this.$emit('update:isShow', false)
        this.$emit('updateList')
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
